<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class Course extends CI_Controller{
    /**
     * 通过课程获取相关教师列表
     * @return [type] [description]
     */
    public function getTeacher(){
        $success=true;
        $str['id']=@$_POST['id']?@$_POST['id']:'';
        $this->load->model('m_course','course',true);
        $data=$this->course->course_teacher_info($str);
        if($data){
            $msg="成功！";
            echo json_encode(array('success'=>$success,"msg"=>$msg,'data'=>$data));
        }else{
            $msg="失败！";
            $success=false;
            echo json_encode(array('success'=>$success,"msg"=>$msg));
        }
    }
    /**
     * 添加课程信息模板
     */
    public function course_add_panel(){
        session_start();
        $this->load->helper("url");
        $base=$this->config->item("base_url");

        echo $this->load->view("admin/course_add_panel",array("base"=>$base));
    }
    /**
     * 添加课程信息
     * @return [type] [description]
     */
    public function course_add(){
        session_start();
        $success=true;
        $this->load->helper("url");
        $base=$this->config->item("base_url");

        $name=@$_POST['name']?@$_POST['name']:'';
        $credit=@$_POST['credit']?@$_POST['credit']:'';
        $time=@$_POST['time']?@$_POST['time']:'';
        $style=@$_POST['style']?@$_POST['style']:'';
        if($name && $credit && $time && $style){
            $this->load->model('m_course','course',true);
            $str['name']=$name;
            $str['credit']=$credit;
            $str['time']=$time;
            $str['style']=$style;
            if($this->course->course_add($str)){
                $msg="添加成功！";
            }else{
                $success=false;
                $msg="添加失败！";
            }
        }else{
            $msg="请填写完整信息！";
            $success=false;
        }
        echo json_encode(array('success'=>$success,'msg'=>$msg));
    }
    /**
     * 课程列表
     * @param  string $str [description]
     * @return [type]      [description]
     */
    public function course_list_panel($str="1-6.html"){
        session_start();
        $this->load->helper("url");
        $base=$this->config->item("base_url");

        $success=true;
        $this->load->library('common');
        $page=$this->common->getPage($str);

        $this->load->model("m_course","course",true);
        $result=$this->course->course_list();
        if($result){
            $pageInfo=$this->common->pageInfo($page[0],$page[1],$result);
        }else{
            $pageInfo='';
        }
        echo $this->load->view("admin/course_list",array("base"=>$base,"pageInfo"=>$pageInfo));
    }
    /**
     * 课程列表分页
     * @param  string $str [description]
     * @return [type]      [description]
     */
    public function course_list_page($str="1-6.html"){
        session_start();
        $this->load->helper("url");
        $base=$this->config->item("base_url");

        $success=true;
        $this->load->library('common');
        $page=$this->common->getPage($str);

        $this->load->model("m_course","course",true);
        $result=$this->course->course_list();
        if($result){
            $pageInfo=$this->common->pageInfo($page[0],$page[1],$result);
        }else{
            $pageInfo='';
        }
        $panel=$this->load->view("admin/course_list_page",array("base"=>$base,"pageInfo"=>$pageInfo),true);
        echo json_encode(array('success'=>$success,'plane'=>$panel));
    }
    /**
     * 查询模板
     * @return [type] [description]
     */
    public function course_search_panel(){
        session_start();
        $this->load->helper("url");
        $base=$this->config->item("base_url");
        echo $this->load->view("admin/course_search_panel",array("base"=>$base));
    }
    /**
     * 查询课程
     * @param  string $str [description]
     * @return [type]      [description]
     */
    public function course_search($str="1-6.html"){
        session_start();
        $this->load->helper("url");
        $base=$this->config->item("base_url");
        $this->load->library("common");
        $success=true;

        $page=$this->common->getPage($str);
        $name=@$_POST['name']?@$_POST['name']:"";
        if(empty($name)){
            $success=false;
            $msg="请填写完整信息！";
        }
        if($success){
            $this->load->model("m_course","course",true);
            $result=$this->course->course_search($name);
            if($result){
                $msg="搜索成功！";
                $pageInfo=$this->common->pageInfo($page[0],$page[1],$result);
                $panel=$this->load->view("admin/course_search_result",array("base"=>$base,"pageInfo"=>$pageInfo),true);
            }else{
                $success=false;
                $msg="没有相关课程！";
            }
        }
        if($success==false){
            $panel='';
        }
        echo json_encode(array("success"=>$success,"msg"=>$msg,"panel"=>$panel));
    }
    /**
     * 课程基本信息跟新
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function course_base_update(){
        $str=array();
        $str['id']=@$_POST['id']?@$_POST['id']:'';
        $str['name']=@$_POST['name']?@$_POST['name']:'';
        $str['credit']=@$_POST['credit']?@$_POST['credit']:'';
        $str['time']=@$_POST['time']?@$_POST['time']:'';

        $this->load->model("m_course","course",true);
        echo $this->course->course_base_update($str);
    }
    /**
     * 删除信息
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function course_delete($params){
        $msg='';
        $success=true;
        $str['id']=$params;

        $this->load->model("m_course",'course',true);
        if($this->course->course_base_delete($str)){
            $msg="成功删除！";
            $this->course->course_plan_delete(array('c_id'=>$params));
        }else{
            $success=false;
            $msg="删除失败！";
        }
        echo json_encode(array('msg'=>$msg,'success'=>$success));
    }
    /**
     * 课程安排模板
     * @return [type] [description]
     */
    public function course_plan_panel(){
        session_start();
        $this->load->helper("url");
        $base=$this->config->item("base_url");

        //获取学院列表
        $this->load->model("m_college","college",true);
        $academy_list=$this->college->getAcademy();
        //获取课程列表
        $this->load->model("m_course","course",true);
        $course_list=$this->course->course_list();
        //选修/必修
        $style[0]='请选择';
        $style[1]='必修';
        $style[2]='选修';

        $this->load->view("admin/course_plan_panel",array("base"=>$base,"academy_list"=>$academy_list,"course_list"=>$course_list,'style'=>$style));
    }
    /**
     * 添加课程安排
     * @return [type] [description]
     */
    public function course_plan_add(){
        session_start();
        $success=true;
        $this->load->helper("url");
        $base=$this->config->item("base_url");

        $str=array();
        $str['c_id']=@$_POST['c_id']?@$_POST['c_id']:'';
        $str['academy']=@$_POST['academy']?@$_POST['academy']:'';
        $str['major']=@$_POST['major']?@$_POST['major']:'';
        $str['period']=@$_POST['grade']?@$_POST['grade']:'';
        $str['semester']=@$_POST['semester']?@$_POST['semester']:'';
        $str['timeLong']=@$_POST['timeLong']?@$_POST['timeLong']:'';
        $str['weekday']=@$_POST['weekday']?@$_POST['weekday']:'';
        $str['section']=@$_POST['section']?@$_POST['section']:'';
        $str['place']=@$_POST['place']?@$_POST['place']:'';
        $str['count']=@$_POST['count']?@$_POST['count']:'';
        $str['t_id']=@$_POST['teacher']?@$_POST['teacher']:'';
        $str['year']=@$_POST['year']?@$_POST['year']:'';

        $this->load->model("m_course",'course',true);
        if($str['year']){
            $style='2';
        }else{
            $style='1';
        }
        if($this->course->course_plan_add($str,$style)){
            $msg="添加成功！";
        }else{
            $msg="添加失败！";
            $success=false;
        }
        echo json_encode(array('success'=>$success,'msg'=>$msg));
    }
    /**
     * 课程安排查询模板
     * @return [type] [description]
     */
    public function plan_search_panel(){
        session_start();
        $this->load->helper("url");
        $base=$this->config->item("base_url");

        //获取学院列表
        $this->load->model("m_college","college",true);
        $academy_list=$this->college->getAcademy();
        //获取课程列表
        $this->load->model("m_course","course",true);
        $course_list=$this->course->course_list();

        $style[0]='请选择';
        $style[1]='必修';
        $style[2]='选修';

        $this->load->view("admin/plan_search_panel",array("base"=>$base,"academy_list"=>$academy_list,"course_list"=>$course_list,'style'=>$style));
    }
    public function plan_search_result($params="1-6.html"){
        session_start();
        $success=true;
        $this->load->helper('url');
        $base=$this->config->item('base_url');
        $this->load->library('common');
        $page=$this->common->getPage($params);

        $str['c_id']=@$_POST['c_id']?@$_POST['c_id']:'';
        $str['period']=@$_POST['period']?@$_POST['period']:'';
        $str['academy']=@$_POST['academy']?@$_POST['academy']:'';
        $str['semester']=@$_POST['semester']?@$_POST['semester']:'';
        $str['major']=@$_POST['major']?@$_POST['major']:'';
        $str['year']=@$_POST['year']?@$_POST['year']:'';

        if(empty($str['c_id'])){
            $success=false;
            $msg="查询条件不满足！";
            $panel='';
        }else{
            $this->load->model('m_course','course',true);
            $course_base_info=$this->course->course_base_info($str['c_id']);

            if($course_base_info[0]->style=='1' && empty($str['year']) ){
                $success=true;
            }else if($course_base_info[0]->style=='2' && empty($str['period']) && empty($str['academy']) && empty($str['semester']) && empty($str['major'])){
                $success=true;
            }else{
                $success=false;
                $msg="查询条件不满足！";
                $panel='';
            }
        }
        if($success){
            $result=$this->course->search_course_info($str,$course_base_info[0]->style);

            $this->load->model('m_college','college',true);
            $this->load->model('m_teacher','teacher',true);

            $semester[1]="大一上学期";
            $semester[2]="大一下学期";
            $semester[3]="大二上学期";
            $semester[4]="大二下学期";
            $semester[5]="大三上学期";
            $semester[6]="大三下学期";
            $semester[7]="大四上学期";
            $semester[8]="大四下学期";

            $weekday[1]="星期一";
            $weekday[2]="星期二";
            $weekday[3]="星期三";
            $weekday[4]="星期四";
            $weekday[5]="星期五";
            $weekday[6]="星期六";
            $weekday[7]="星期日";

            $section[1]="第一节";
            $section[2]="第二节";
            $section[3]="第三节";
            $section[4]="第四节";
            $section[5]="第五节";

            if($result){
                if($result[0]->style!='2'){
                    foreach ($result as $key => $value) {
                        $academy=$this->college->academy(array('p_id'=>'0','number'=>$value->academy));
                        $major=$this->college->academy(array('p_id'=>$academy[0]->co_id,'number'=>$value->major));
                        $teacher=$this->teacher->teacher_search(array('t_id'=>$value->t_id));

                        $arr[]=array($value->id,$value->name,$academy[0]->name,$major[0]->name,$value->period,$semester[$value->semester],$value->timeLong,$weekday[$value->weekday],$section[$value->section],$value->place,$teacher[0]->name);
                    }
                    $msg="搜索成功！";
                    $pageInfo=$this->common->pageInfo($page[0],$page[1],$arr);
                    $panel=$this->load->view("admin/plan_search_result",array("base"=>$base,"pageInfo"=>$pageInfo),true);
                }else{
                    foreach ($result as $key => $value) {
                        $teacher=$this->teacher->teacher_search(array('t_id'=>$value->t_id));
                        if($teacher){
                            $value->teacher=$teacher[0]->name;
                        }else{
                            $value->teacher='无';
                        }
                        $value->weekday=$weekday[$value->weekday];
                        $value->section=$section[$value->section];
                    }
                    $msg="搜索成功！";
                    $pageInfo=$this->common->pageInfo($page[0],$page[1],$result);
                    $panel=$this->load->view("admin/plan_search_two_result",array("base"=>$base,"pageInfo"=>$pageInfo),true);
                }
            }else{
                $success=false;
                $msg="没有相关课程！";
                $panel='';
            }
        }
        echo json_encode(array("success"=>$success,"msg"=>$msg,"panel"=>$panel));
    }
    /**
     * 选修安排修改模板
     * @param  [type] $param [description]
     * @return [type]        [description]
     */
    public function elective_course_update_panel($param){
        session_start();
        $this->load->helper("url");
        $base=$this->config->item("base_url");

        //课程名称
        $name=@$_POST['title']?@$_POST['title']:'';

        $this->load->model('m_teacher','teacher',true);

        //获取课程列表
        $this->load->model("m_course","course",true);

        $result=$this->course->elective_course_plan_search(array('id'=>$param));

        $semester[1]="大一上学期";
        $semester[2]="大一下学期";
        $semester[3]="大二上学期";
        $semester[4]="大二下学期";
        $semester[5]="大三上学期";
        $semester[6]="大三下学期";
        $semester[7]="大四上学期";
        $semester[8]="大四下学期";

        $weekday[1]="星期一";
        $weekday[2]="星期二";
        $weekday[3]="星期三";
        $weekday[4]="星期四";
        $weekday[5]="星期五";
        $weekday[6]="星期六";
        $weekday[7]="星期日";

        $section[1]="第一节";
        $section[2]="第二节";
        $section[3]="第三节";
        $section[4]="第四节";
        $section[5]="第五节";

        $this->load->view("admin/elective_course_update_panel",array("base"=>$base,'weekday'=>$weekday,'section'=>$section,'name'=>$name,"course_plan_list"=>$result));
    }
    /**
     * 必修安排修改模板
     * @return [type] [description]
     */
    public function course_update_panel($param){
        session_start();
        $this->load->helper("url");
        $base=$this->config->item("base_url");

        //课程名称
        $name=@$_POST['title']?@$_POST['title']:'';

        $this->load->model('m_teacher','teacher',true);

        //获取课程列表
        $this->load->model("m_course","course",true);

        $result=$this->course->course_plan_search(array('id'=>$param));

        $semester[1]="大一上学期";
        $semester[2]="大一下学期";
        $semester[3]="大二上学期";
        $semester[4]="大二下学期";
        $semester[5]="大三上学期";
        $semester[6]="大三下学期";
        $semester[7]="大四上学期";
        $semester[8]="大四下学期";

        $weekday[1]="星期一";
        $weekday[2]="星期二";
        $weekday[3]="星期三";
        $weekday[4]="星期四";
        $weekday[5]="星期五";
        $weekday[6]="星期六";
        $weekday[7]="星期日";

        $section[1]="第一节";
        $section[2]="第二节";
        $section[3]="第三节";
        $section[4]="第四节";
        $section[5]="第五节";

        //获取学院列表
        $this->load->model("m_college","college",true);
        $academy_list=$this->college->getAcademy();
        $academy=$this->college->academy(array('p_id'=>'0','number'=>$result[0]->academy));
        $major=$this->college->academy(array('p_id'=>$academy[0]->co_id,'number'=>$result[0]->major));
        $major_list=$this->college->academy(array('p_id'=>$academy[0]->co_id));
        $teacher_list=$this->course->course_teacher_info(array('c_id'=>$result[0]->c_id));
        $teacher=$this->teacher->teacher_search(array('t_id'=>$result[0]->t_id));

        $this->load->view("admin/course_update_panel",array("base"=>$base,"academy_list"=>$academy_list,"academy"=>$academy,'major_list'=>$major_list,'major'=>$major,"course_plan_list"=>$result,'semester'=>$semester,'weekday'=>$weekday,'section'=>$section,'teacher_list'=>$teacher_list,'teacher'=>$teacher,'name'=>$name));
    }
    /**
     * 安排跟新
     * @return [type] [description]
     */
    public function course_plan_update(){
        session_start();
        $success=true;
        $this->load->helper("url");
        $base=$this->config->item("base_url");

        $where=array();
        $str=array();
        $where['id']=@$_POST['id']?@$_POST['id']:'';

        $str['academy']=@$_POST['academy']?@$_POST['academy']:'';
        $str['major']=@$_POST['major']?@$_POST['major']:'';
        $str['period']=@$_POST['grade']?@$_POST['grade']:'';
        $str['semester']=@$_POST['semester']?@$_POST['semester']:'';
        $str['timeLong']=@$_POST['timeLong']?@$_POST['timeLong']:'';
        $str['weekday']=@$_POST['weekday']?@$_POST['weekday']:'';
        $str['section']=@$_POST['section']?@$_POST['section']:'';
        $str['place']=@$_POST['place']?@$_POST['place']:'';
        $str['count']=@$_POST['count']?@$_POST['count']:'';
        $str['t_id']=@$_POST['teacher']?@$_POST['teacher']:'';

        $this->load->model("m_course",'course',true);
        if($str['count']){
            $style='2';
        }else{
            $style='1';
        }
        if($this->course->course_plan_update($str,$where,$style)){
            $msg="修改成功！";
        }else{
            $msg="修改失败！";
            $success=false;
        }
        echo json_encode(array('success'=>$success,'msg'=>$msg));
    }
    /**
     * 删除安排
     * @param  [type] $param [description]
     * @return [type]        [description]
     */
    public function delete_plan($param){
        session_start();
        $this->load->helper('url');
        $base=$this->config->item('base_url');

        $success=true;
        $msg='';
        $this->load->model('m_course','course',true);
        if($this->course->course_plan_delete(array('id'=>$param))){
            $msg="成功删除！";
        }else{
            $msg="删除失败！";
        }
        echo json_encode(array('success'=>$success,'msg'=>$msg));
    }
}